package com.ss.task.rest;

import com.ss.task.dto.UserInfo;
import com.ss.task.model.DepartmentEntity;
import com.ss.task.model.LeaderEntity;
import com.ss.task.model.UserEntity;
import com.ss.task.service.UserService;
import com.ss.task.util.ErrorInfo;
import com.ss.webutil.struct.BaseResult;
import com.ss.webutil.struct.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by liymm on 2015-03-23.
 */
@RestController
@RequestMapping("/rest/user/")
public class UserController {

    @Autowired
    UserService userService;


    @RequestMapping("login")
    public ResultModel<UserEntity> loginUser(String name, String password,HttpSession session) {

        if (name == null)
            return new ResultModel<UserEntity>(ErrorInfo.INVALIE_NAME_OR_PASSWORD);
        else if(password == null)
            return new ResultModel<UserEntity>(ErrorInfo.INVALIE_NAME_OR_PASSWORD);
        else {
            ResultModel<UserEntity> r = userService.login(name, password);

            if(r.failed()){
                return new ResultModel<UserEntity>(ErrorInfo.INVALIE_NAME_OR_PASSWORD);
            }
            UserEntity user = r.getData();

            session.setAttribute("user", user);

            List<LeaderEntity> leaders = userService.getLeaderByUser(user.getId()).getData();


            session.setAttribute("isLeader", leaders.size() != 0);

            if (leaders.size() == 0) {
                r.getData().setLeader(false);
            } else {
                r.getData().setLeader(true);
            }
            return r;
        }
    }



    @RequestMapping("subordinate")
    public ResultModel<List<UserEntity>> subordinate(Integer userId) {

       if(userId == null)
           return new ResultModel<List<UserEntity>>(ErrorInfo.INVALID_PARAMETER);

        List<LeaderEntity> leaders = userService.getLeaderByUser(userId).getData();

        if(leaders.size() == 0){
            return new ResultModel<List<UserEntity>>(ErrorInfo.INVALID_PARAMETER);
        }
        else{
            return userService.getUsersByLeader(userId);
        }
    }

    @RequestMapping("changePassword")
    public BaseResult changePass(Integer id,String newp,String again){
          UserEntity user =   userService.getUser(id).getData();
          String old = user.getPassword();
          return userService.changePwd(id,old,newp,again);
    }



    @RequestMapping("logout")
    public void logout(HttpSession session){
        session.invalidate();
    }


    @RequestMapping("add")
    public ResultModel<UserEntity> addUser(String name, String realname, Integer admin) {

        if (name == null)
            return new ResultModel<UserEntity>(ErrorInfo.INVALID_PARAMETER);

        if (realname == null)
            return new ResultModel<UserEntity>(ErrorInfo.INVALID_PARAMETER);

        return userService.addUser(name, realname, null, null, admin);
    }

    @RequestMapping("list")
    public ResultModel<List<UserEntity>> listUser(Integer offset, Integer length) {

        if (offset == null)
            return new ResultModel<List<UserEntity>>(ErrorInfo.INVALID_PARAMETER);

        if (length == null)
            return new ResultModel<List<UserEntity>>(ErrorInfo.INVALID_PARAMETER);

        return userService.getUsers(offset, length);
    }

    @RequestMapping("listcount")
    public ResultModel<Long> countOfListUser() {
        return userService.getCountOfUsers();
    }

    @RequestMapping("remove")
    public BaseResult removeUser(Integer id) {

        if (id == null)
            return new BaseResult(ErrorInfo.INVALID_PARAMETER);

        return userService.removeUser(id);
    }

    @RequestMapping("modify")
    public ResultModel<UserEntity> modifyUser(Integer id, String realname, Integer did, String title) {

        if (did < 0)
            did = null;

        return userService.modifyUser(id, realname, did, title);
    }

    @RequestMapping("{id}")
    public ResultModel<UserInfo> getInfo(@PathVariable Integer id) {
        ResultModel<UserEntity> u = userService.getUser(id);

        if (u.getStatus() != ResultModel.SUCCESS)
            return new ResultModel<UserInfo>(ErrorInfo.INVALID_PARAMETER);

        ResultModel<List<DepartmentEntity>> d = userService.getDepts();

        return new ResultModel(new UserInfo(u.getData(), d.getData()));
    }

    @RequestMapping("{id}/dept/lead")
    public ResultModel<List<LeaderEntity>> getLeadDept(@PathVariable Integer id) {
        return userService.getLeaderByUser(id);
    }

    @RequestMapping("{id}/follower")
    public ResultModel<List<UserEntity>> getFollower(@PathVariable Integer id) {
        return userService.getUsersByLeader(id);
    }

    @RequestMapping("password/modify")
    public BaseResult modifyPassword(Integer id, String oldpw, String newpw, String reppw) {
        return userService.changePwd(id, oldpw, newpw, reppw);
    }

}
